#!/usr/bin/python
# coding:utf-8

import time
import json
import requests


def publicHeader():
    headers = {
        'Host': 'glibra.bgex.top',
        'someDeviceInfo': 'deviceName=iPhone&systemName=iOS 13.4&model=iPhone&isWifi=1&isSIMInstalled=1',
        'Accept-Language': 'zh-cn',
        'lang': 'zh_CN',
        'User-Agent': 'GLibraPlanet/40 CFNetwork/1125.2 Darwin/19.4.0',
        'deviceNo': '7A2516F2-E96F-4177-AC18-F075A90B22BD',
        'device-model': 'iPhone X',
        'Accept': '*/*',
        'app-version': '1.1.7',
        'os-version': '13.4',
        'source-terminal': 'ios',
    }

    return headers

def timestamp():
    return str(int(round(time.time() * 1000)))



def print_response(response):
    try:
        data = json.loads(response.content)
        print(json.dumps(data, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False))
    except Exception as e:
        print('json数据格式不正确：' + str(e))



def didLogin(username,password):

    headers = publicHeader()
    headers['GLSign'] = '++jOgDx+4HT5zsmKnYWHlGaAO3hnYGiCyb1W3FkSK/4='
    headers['enable'] = 'false'
    headers['timestamp'] = '1585559193831'
    headers['Content-Type'] = 'application/json'

    url = 'http://glibra.bgex.top/v2/w/login'
    password = '18e874ff15e4e42c8edc630fa6b369aa'
    dict = {"countryCode":"+86","lang":"zh_CN","password":password,"username":username}
    data = json.dumps(dict)

    session = requests.Session()
    response = session.post(url, headers=headers, data=data)
    print_response(response)
    return response

def login(username):
    password = '18e874ff15e4e42c8edc630fa6b369aa'
    return didLogin(username,password)


def useable(cookies):

    del cookies['sec_tc']
    headers = publicHeader()
    url = 'http://glibra.bgex.top/v2/w/mining/backpack/usable'
    response = requests.get(url, headers=headers, cookies=cookies)
    print_response(response)
    # rs = json.loads(response.content)
    # data = rs['data']
    # return data



def hammer(cookies,authorization,glSign):
    # data = useable(cookies)
    response = test()
    rs = json.loads(response.content)

    if rs['errcode'] == 200:
        data = rs['data']
        for dict in data:
            hammerId = dict['hammerId']
            usableNumber = dict['usableNumber']
            for i in range(2, usableNumber):  # 根据因子迭代
                didHammer(cookies, authorization, glSign, hammerId)
    else:
        print_response(response)


def didHammer(cookies,authorization,GLSign,hammerId):

    headers = publicHeader()
    headers['Authorization'] = authorization
    headers['GLSign'] = GLSign
    headers['timestamp'] = timestamp()
    # 'http://glibra.bgex.top/v2/w/mining/hammer/use/13	小型威力锤'
    # 'http://glibra.bgex.top/v2/w/mining/hammer/use/14	中型威力锤'
    # 'http://glibra.bgex.top/v2/w/mining/hammer/use/15	大型威力锤'
    # url = 'http://glibra.bgex.top/v2/w/mining/hammer/use/14'



    url = 'http://glibra.bgex.top/v2/w/mining/hammer/use/' + str(hammerId)

    response = requests.put(url, headers=headers, cookies=cookies)

    if hammerId == 13:
        print "小型威力锤挖矿成功"
    elif hammerId == 14:
        print "中型威力锤挖矿成功"
    elif hammerId == 15:
        print "大型威力锤挖矿成功"

    print_response(response)

def test():
    cookies = {
        'JSESSIONID': 'PJSOKWidHbWNGXdMazAd4L0eEiYY9zVx6ns8WR7o',
        'SERVERID': '0ab77026abaf194bf7d5fe6f4024f8e8|1585565902|1585563568',
    }

    headers = {
        'Host': 'glibra.bgex.top',
        'deviceNo': '7A2516F2-E96F-4177-AC18-F075A90B22BD',
        'Accept': '*/*',
        'Authorization': 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMzc2MDM1NDcyNiIsImF1dGgiOiJST0xFX1VTRVIiLCJpZCI6MTI0NDQ3OTY0NjYzMjI5NjQ1MCwidGVsIjoiMTM3NjAzNTQ3MjYiLCJjb3VudHJ5X2NvZGUiOiIrODYiLCJleHAiOjE1ODU3MDk4ODV9.vGjFlqz10rIa7W1_Zqhnjuaw_LwYpL-f_Uzs6O81Ou42Of_w6NBJ1swlBHK8skl7m4HAsBM6gG-KEOk1K_XBWw',
        'timestamp': '1585566289426',
        'app-version': '1.1.7',
        'someDeviceInfo': 'deviceName=iPhone&systemName=iOS 13.4&model=iPhone&isWifi=1&isSIMInstalled=1',
        'Accept-Language': 'zh-cn',
        'GLSign': 'ANq5jSzlI7hs9CTsL0kKSeivcLF1tHPCq6RjG2mgPGU=',
        'source-terminal': 'ios',
        'device-model': 'iPhone X',
        'lang': 'zh_CN',
        'User-Agent': 'GLibraPlanet/40 CFNetwork/1125.2 Darwin/19.4.0',
        'os-version': '13.4',
    }

    response = requests.put('http://glibra.bgex.top/v2/w/mining/hammer/use/13', headers=headers, cookies=cookies)
    # print_response(response)

    return response

def finishTask(account):

# 登录
    response = login(str(account))

    cookies = requests.utils.dict_from_cookiejar(response.cookies)
    # print cookies

    rs = json.loads(response.content)

    authorization = str(rs['data']['access_token'])
    glSign = 'J6OqF5XD9JkGE7+xv6snGEbfpaMGIA/nQmGTCXRzX9I='

    hammer(cookies,authorization,glSign)

    print str(account) + ' 完成任务'


if __name__ == '__main__':
     finishTask(13760354726)
    # time.sleep(60)
    # test()